﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Windows.Data;


namespace TimeReporter
{
    public partial class frmStart : Form
    {
        
        ProjectList theProjects = ProjectList.getInstance();

        

        
       // TaskList theTasks = TaskList.GetInstance();

        public frmStart()
        {
            InitializeComponent();
                     
            
            
            
            //checkedListBox1.SelectedValueChanged += new EventHandler(checkedListBox1_SelectedValueChanged);

            //filteredTasks2.RaiseListChangedEvents = true;
            /*
            filteredTasks2.AddingNew += new AddingNewEventHandler(filteredTasks2_AddingNew);
            filteredTasks2.ListChanged += new ListChangedEventHandler(filteredTasks2_ListChanged);

            test.RaiseListChangedEvents = true;
            test.AddingNew += new AddingNewEventHandler(test_AddingNew);
            test.ListChanged += new ListChangedEventHandler(test_ListChanged);

            test.Add(1);
            test.Remove(1);

            test2.ListChanged += new ListChangedEventHandler(test2_ListChanged);

            test2.Add(1);
            test2.Remove(1);
            */
            
        }

        void test2_ListChanged(object sender, ListChangedEventArgs e)
        {
            Console.WriteLine("test2 changed, " + e.ListChangedType.ToString());
        }

        void test_ListChanged(object sender, ListChangedEventArgs e)
        {
            Console.WriteLine("test added, " + e.ListChangedType.ToString());
        }

        void test_AddingNew(object sender, AddingNewEventArgs e)
        {
            throw new NotImplementedException();
        }

        void filteredTasks2_ListChanged(object sender, ListChangedEventArgs e)
        {
            Console.WriteLine("List changed" + e.ListChangedType.ToString());
        }

        void filteredTasks2_AddingNew(object sender, AddingNewEventArgs e)
        {
            Console.WriteLine("List changed");
        }

        private void btnSelectFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog ofd = new System.Windows.Forms.FolderBrowserDialog();
            DialogResult theResult = ofd.ShowDialog();
            if (theResult == System.Windows.Forms.DialogResult.OK)
            {
                theProjects.PathToFiles = ofd.SelectedPath;
            }
        }

        private void btnGetData_Click(object sender, EventArgs e)
        {
            //folderReader.populateLists(theTasks, theProjects);
            folderReader.populateSingleList(theProjects);
           
            //dataGridView1.Columns.Remove("TaskId");
           // dataGridView1.Columns.Remove("UserId");
            
            Console.WriteLine("Done");
        }


        

        private void checkedListBox1_SelectedValueChanged(object sender, EventArgs e)
        {

            
            
        }

        

        private void listBox1_SelectedValueChanged(object sender, EventArgs e)
        {
           
            
            
            // Cycle through all selected items and update the dataview accordingly
            /*
            var xxx =
                from c in ds2.timeSlipDataTableTyped
                where c.Project.Contains("sdsd!")
                select c;

            



            timeSlipList = null;
            foreach (DataRowView row in listBox1.SelectedItems)
            {
                string selItem = row.Row["Project"].ToString();
                
                var Query =
                from c in ds2.timeSlipDataTableTyped
                where c.Project == "ssd"
                // ["Project"].ToString() == listBox1.SelectedValue.ToString()
                select c;

                
                
                foreach (DataRow row2 in Query)
                {
                    

                }


            }

            
            

            var test =
                from c in ds2.timeSlipDataTableTyped
                where listBox1.SelectedItems.Contains(c.Project)
                select c;
            
            DataRowView nisse = (DataRowView)listBox1.SelectedItem;

           // string zz = nisse.Row["Project"].ToString();
            
            
                


            /*    
            foreach (string x in Query)
                {
                    Console.WriteLine(x);
                }
            */

                /*
           
            timeSlipList = test.AsDataView();
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = timeSlipList;
            
            */
            
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            timeSlipDataBS.Filter = filterData();
        }

        

        private void lstProjects_SelectedValueChanged(object sender, EventArgs e)
        {
            timeSlipDataBS.Filter = filterData();
        }

        private string filterData()
        {
            string filter = "";
            string projectFilter = "";
            string startDateFilter = "";
            string endDateFilter = "";
            
            
            // First create filter on project

            List<string> xx = new List<string>();
            foreach (DataRowView row in lstProjects.SelectedItems)
            {
                xx.Add(row.Row["Project"].ToString());
                //projectFilter = projectFilter + " OR " + "Project = '" + selItem + "'";
            }

            projectFilter = "Project ='" +xx[0] + "'";
            for (int i = 1; i <= xx.Count-1; i++)
            {
                projectFilter = projectFilter + " OR " + "Project ='" +xx[i] +"'";

            }
            if (projectFilter.Contains("Project ='All'"))
            {
                projectFilter = "";
            }
            

            // Next create filter on start date

            startDateFilter = "StartDateTime >= '" + dateTimePicker1.Value.ToShortDateString() + " 00:00:00'";


            // ..and finally create filter on end date

            endDateFilter = "StartDateTime <= '" + dateTimePicker2.Value.ToShortDateString() +" 23:59:59'";


            // Filter the dataset

            if (projectFilter != "")
            {
                filter = "(" + projectFilter + ") AND (" + startDateFilter + " AND " + endDateFilter + ")";

            }
            else
            {
                filter = startDateFilter + " AND " + endDateFilter;
            }

            return filter;
            
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            timeSlipDataBS.Filter = filterData();
        }

        private void btnClearFilter_Click(object sender, EventArgs e)
        {
            timeSlipDataBS.RemoveFilter();
        }

        private void btnSummarize_Click(object sender, EventArgs e)
        {
            //var Query =
            //    from o in timeSlipDataBS.Ta
            // Test
                 
        }

        private void bindingSource1_DataSourceChanged(object sender, EventArgs e)
        {
            Console.WriteLine("Datasource changed");
        }

        
        

        
        

       
        

    }
}
